<template>
    <el-dialog v-model="visible" title="建档信息" :close-on-click-modal="false">
        <el-form ref="dataFormRef" label-width="220" :label-position="'top'" :model="dataForm" :rules="dataRules">
            <el-form-item label="建档医疗机构" prop="crtRcdOrgCd">
                <el-select v-model="dataForm.crtRcdOrgCd" filterable placeholder="请选择医疗机构名称" style="width: 100%;">
                    <el-option v-for="(item, index) in orgList" :key="index" :label="item.name"
                        :value="item.orgCode"></el-option>
                </el-select>
            </el-form-item>
            <el-form-item label="建档医生" prop="crtRcdDctNm">
                <el-input v-model="dataForm.crtRcdDctNm" maxlength="50" placeholder="请输入建档医生"></el-input>
            </el-form-item>

            <el-form-item label="建档日期" prop="crtRcdDt">
                <el-date-picker v-model="dataForm.crtRcdDt" type="date" placeholder="请选择建档日期" style="width: 100%" />
            </el-form-item>
            <el-form-item label="建档生效日期" prop="crtRcdStrtDt">
                <el-date-picker v-model="dataForm.crtRcdStrtDt" type="date" placeholder="请选择建档生效日期" style="width: 100%" />
            </el-form-item>
            <el-form-item label="建档状态" prop="rcdSttsCd">
                <el-switch v-model="dataForm.rcdSttsCd" active-text="正常" inactive-text="异常" active-value="0"
                    inactive-value="1" />
            </el-form-item>
        </el-form>
        <template #footer>
            <el-button @click="visible = false">取消</el-button>
            <el-button type="primary" :loading="btnLoading" @click="subFormHandle">确定</el-button>
        </template>
    </el-dialog>
</template>

<script lang="ts" setup>
import { nextTick, reactive, ref } from 'vue';
import timeFormater from 'time-formater'
import { saveFile, useOrgListApi } from '@/api/patient-primary/patient-mana'
import { ElMessage } from 'element-plus';

const visible = ref(false)
const btnLoading = ref(false)
const dataFormRef = ref()
const emit = defineEmits(['close'])
const dataRules = ref({
    crtRcdOrgCd: [{ required: true, message: '请选择建档机构', trigger: 'blur' }],
    crtRcdDctNm: [{ required: true, message: '请输入建档医生', trigger: 'blur' }],
    crtRcdDt: [{ required: true, message: '请选择建档日期' }],
    crtRcdStrtDt: [{ required: true, message: '请选择建档生效日期' }],
})

const dataForm = reactive({
    crtRcdOrgNm: '',
    crtRcdDctNm: '',
    rcdSttsCd: '0',
    crtRcdDt: '',
    crtRcdStrtDt: '',
})

const orgList = ref([])

const init = async (data, empiId) => {
    visible.value = true
    btnLoading.value = false

    await useOrgListApi().then((res) => {
        orgList.value = res.data
    })

    nextTick(() => {
        if (dataFormRef.value) {
            dataFormRef.value.resetFields()
        }

        dataForm.empi = empiId

        if (data) {
            Object.assign(dataForm, data)
        } else {
            dataForm.id = ''
        }
    })
}

const subFormHandle = () => {
    dataFormRef.value.validate((valid) => {
        if (!valid) {
            return false
        }
        btnLoading.value = true

        if (dataForm.crtRcdDt) {
            dataForm.crtRcdDt = timeFormater(dataForm.crtRcdDt).format('YYYY-MM-DD')
        }
        if (dataForm.crtRcdStrtDt) {
            dataForm.crtRcdStrtDt = timeFormater(dataForm.crtRcdStrtDt).format('YYYY-MM-DD')
        }


        dataForm.crtRcdOrgNm = orgList.value.filter((item) => item.orgCode == dataForm.crtRcdOrgCd)[0].name

        saveFile(dataForm).then(() => {
            ElMessage.success({
                message: '保存成功',
                duration: 500,
                onClose: () => {
                    btnLoading.value = false
                    visible.value = false
                    emit('close')
                }
            })
        }).catch(() => {
            btnLoading.value = false
        })
    })
}

defineExpose({
    init
})
</script>

<style lang="scss" scoped></style>